package at.jku.ssw.fortress.ui;

import at.jku.ssw.fortress.ui.assistant.OperatorShorthands;


public class AsciiUnicodeConverter {
	
	private Bidirection<String, Character> fun = new Bidirection<String, Character>();
	
	public AsciiUnicodeConverter() {
		add(OperatorShorthands.encodedEnclosing);
		add(OperatorShorthands.encodedLeft);
		add(OperatorShorthands.encodedOps);
		add(OperatorShorthands.encodedL2R);
	}
	
	private void add(Shorthand[] shorthands) {
		for(Shorthand s : shorthands) {
			fun.setFunctionValue(s.name, (char) Integer.parseInt(s.unicode, 16));
		}
	}
	
	public Character getUnicode(String ascii) {
		Character res = fun.value(ascii);
		if(res != null) {
			return res;
		}
		if(ascii.length() == 1) {
			return ascii.charAt(0);
		}
		return null;
	}
	
	public String getAscii(Character unicode) {
		String res = fun.inverse(unicode);
		if(res != null) {
			return res;
		}
		return null;
	}
	
	public String getAscii(String unicode) {
		StringBuffer res = new StringBuffer();
		for(char c : unicode.toCharArray()) {
			String ascii = getAscii(c);
			if(ascii != null) {
				res.append(ascii);
			} else {
				res.append(c);
			}
		}
		return res.toString();
	}
	
}
